package vn.vietel.pms.business;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

import org.springframework.transaction.annotation.Transactional;

import vn.vietel.pms.entity.VGroupOfProperty;
import vn.vietel.pms.entity.VKindOfProperty;
import vn.vietel.pms.entity.VListOfProperty;
import vn.vietel.pms.entity.VTypeOfProperty;
import vn.vietel.pms.service.ListPropertyService;

public class ListPropertyServiceImpl implements ListPropertyService {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void create(VListOfProperty item) {
		if (item != null) {
			entityManager.persist(item);
		}
	}

	@Transactional
	public VListOfProperty find(Long id) {
		return entityManager.find(VListOfProperty.class, id);
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Transactional
	public void delete(VListOfProperty item) {
		entityManager.remove(item);

	}

	@Transactional
	public VListOfProperty find(String name) {
		try {
			return (VListOfProperty) entityManager
					.createQuery(
							"select g from VListOfProperty g where g.propertyName=:name")
					.setParameter("name", name).getSingleResult();
		} catch (PersistenceException e) {
			return null;
		}
	}

	@Transactional
	public void update(VListOfProperty item) {
		entityManager.merge(item);

	}

	@SuppressWarnings("unchecked")
	public List<VListOfProperty> getAll() {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VListOfProperty g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<VListOfProperty> getAll(VGroupOfProperty group) {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VListOfProperty g where g.groupId = :groupId");
			return qr.setParameter("groupId", group.getId()).getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<VListOfProperty> getAll(VTypeOfProperty type) {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VListOfProperty g where g.typeId = :typeId");
			return qr.setParameter("typeId", type.getId()).getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<VListOfProperty> getAll(VKindOfProperty kind) {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VListOfProperty g where g.kindId = :kindId");
			return qr.setParameter("kindId", kind.getId()).getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<VGroupOfProperty> getAllGroups() {
		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VGroupOfProperty g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<VKindOfProperty> getAllKinds() {
		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VKindOfProperty g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<VTypeOfProperty> getAllTypes() {
		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VTypeOfProperty g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}
	
	
}
